import requests
import re
import json
import time
import csv


def money():

    for i in range(1,500):
        print(f"正在爬取{i}页")
        num = i
        url = f"https://datacenter-web.eastmoney.com/api/data/v1/get?sortColumns=NOTICE_DATE%2CSUM%2CRECEIVE_START_DATE%2CSECURITY_CODE&sortTypes=-1%2C-1%2C-1%2C1&pageSize=50&pageNumber={num}&reportName=RPT_ORG_SURVEYNEW&columns=SECUCODE%2CSECURITY_CODE%2CSECURITY_NAME_ABBR%2CNOTICE_DATE%2CRECEIVE_START_DATE%2CRECEIVE_PLACE%2CRECEIVE_WAY_EXPLAIN%2CRECEPTIONIST%2CSUM&quoteColumns=f2~01~SECURITY_CODE~CLOSE_PRICE%2Cf3~01~SECURITY_CODE~CHANGE_RATE&quoteType=0&source=WEB&client=WEB&filter=(NUMBERNEW%3D%221%22)(IS_SOURCE%3D%221%22)(RECEIVE_START_DATE%3E%272021-09-07%27)"
        response = requests.get(url=url)
        content = response.content.decode()
        # content = content[44:-2]
        # content = response.text[44:-2]
        # print(content, type(content))
        results = json.loads(content)['result']['data']
        # print(results)
        for result in results:
            id = result['SECURITY_CODE']
            name = result['SECURITY_NAME_ABBR']
            correlation = result['NOTICE_DATE']
            New_price = result['RECEIVE_START_DATE']
            zhang = result['RECEIVE_PLACE']
            number = result['RECEIVE_WAY_EXPLAIN']
            manner = result['RECEPTIONIST']
            place = result['SUM']
            day = result['CLOSE_PRICE']
            days = result['CHANGE_RATE']
            with open(f"./东方财富/东方.csv", "a", encoding="utf8", newline="") as f:
                head = (id, name, correlation, New_price, zhang, number, manner, place, day, days)
                csv_write = csv.writer(f)
                csv_write.writerow(head)
        time.sleep(1)




money()

